<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    //  后台分类控制器

    //后台列表页显示
    public function getIndex(Request $request)
    {
    	//获取主页模糊查询数据
    	$num = $request->input('num',10);
		$likes = $request->input('likes');
		//判断是否有查询条件语句
		if($likes){
            $like = '%'.$likes.'%';
			$cates = DB::table('shop_type')->select(DB::raw(" *,concat(path,',',tid) as paths"))->where('tname','like',$like)->orderBy('paths', 'asc')->paginate($num);
		}else{
			$cates = DB::table('shop_type')->select(DB::raw(" *,concat(path,',',tid) as paths"))->orderBy('paths', 'asc')->paginate($num);
		}
        // dd($cates);
		//查询 所属分类 
		foreach ($cates as $k => $v) {
			//设置字段xname
			$cates[$k]->xname = DB::table('shop_type')->where('tid',$v->pid)->value('tname');
		}
		// 获取参数列表
		$list = $request->all();
    	//设置tname格式
    	$cates = self::getCates($cates);
    	// dd($cates);
    	//解析模板
    	return view('admin.Cate.index',['cates'=>$cates,'list'=>$list,'num'=>$num,'likes'=>$likes]);
    }

	//显示分类添加页面
    public function getAdd()
    {
    	//获取数据
    	$res = DB::select("select *,concat(path,',',tid) as paths from shop_type order by paths");
    	//设置格式
    	$cates = self::getCates($res);
    	//解析模板
    	return view('admin.Cate.add',['cates'=>$cates]);
    }
    //分类添加数据处理
    public function postInsert(Request $request)
    {
    	
    	$data = $request->only('tname','pid');
    	//路径path的获取
    	if($data['pid'] == '0'){
    		$data['path'] = '0';
    	}else{
    		//子类
    		$data['path'] = DB::table('shop_type')->where('tid',$data['pid'])->value('path').','.$data['pid'];
    	}
    	// dd($data);
    	//加入数据库
    	$res = DB::table('shop_type')->insert($data);
    	if($res){
    		//成功
    		return redirect('/admin/cate/index')->with('success','添加成功');
    	}else{

    		//失败
    		return back()->with('error','添加失败');
    	}
    }
    //分类显示格式  设置样式  数据库数据格式设置
    public static function getCates($res)
    {
    	//遍历
    	foreach($res as $k=>$v){
    		//把path变为数组
    		$arr = explode(',',$v->path);
    		//长度-1
    		$len = count($arr)-1;
    		//设置显示的格式
    		$res[$k]->tname = str_repeat('|-->',$len).$v->tname;
    	}
    	return $res;
    }
    //分类删除ajax
    public function postDel(Request $request)
    {
    	$id = $request->input('id');
    	//判断是否有子类
    	if(DB::table('shop_type')->where('pid',$id)->first()){
            echo 0;
    		return;
    	}
    	//判断是否有商品
    	if(DB::table('shop_goods')->where('tid',$id)->first()){
            echo 2;
    		return;
    	}
   		$res = DB::table('shop_type')->where('tid', $id)->delete();
   		echo $res;
    }
    //分类修改  显示修改页面
    public function getEdit(Request $request)
    {
    	$id = $request->input('id');

    	//获取数据
    	//所有分类数据
    	$res = DB::select("select *,concat(path,',',tid) as paths from shop_type  order by paths");
    	//要修改的分类数据
    	$cat = DB::table('shop_type')->where('tid',$id)->first();
    	//设置格式
    	$cates = self::getCates($res);
    	// 解析模板
    	return view('admin.Cate.edit',['cates'=>$cates,'cat'=>$cat]);
    }
    //分类修改数据处理
    public function postUpdate(Request $request)
    {
    	
    	// dd($request->all());
    	$data = $request->only('tname');
    	//执行修改
    	$res = DB::table('shop_type')->where('tid', $request->input('tid'))->update($data);
    	if($res){
    		//成功
    		return redirect('/admin/cate/index')->with('success','修改成功');
    	}else{
    		//失败
    		return back()->with('error','修改失败');
    	}
    }


}
